package ideamaps.action;


import ideamaps.domain.User;
import ideamaps.manager.UserManager;

import com.opensymphony.xwork2.Preparable;
import com.taillive.commons.TextUtils;
import com.taillive.core.web.StrutsAction;

public class RegisterUpdateAction extends StrutsAction implements Preparable{
	
	private User user;
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	private String oldPassword;
	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	
	private String newPassword;
	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	
	
	private UserManager userManager;
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
		
	public String registerUpdate(){
		
		
		if(isGet()){
			return INPUT;
		}else{
			//String screenName = getParameter("user.screenName");
			//user.setScreenName(screenName);
			userManager.update(user);
			
			return SUCCESS;
		}
	}

	public String updatePassword(){
		user.setPassword(newPassword);
		userManager.update(user);
		return SUCCESS;
	}
	
	public void validateUpdatePassword(){

		
		if(oldPassword==null || !oldPassword.equals(user.getPassword())){
			addActionError("oldPassword is invalid");
		}
	}

	public void prepare() throws Exception {
		String username = (String)getSession().getAttribute("sessionUsername");
		user = userManager.getByUsername(username);
	}
}



